package org.dromara.autocode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.autocode.constant.UserConstants;
import org.dromara.autocode.domain.entity.SysAutoCodeRule;
import org.dromara.autocode.mapper.SysAutoCodeRuleMapper;
import org.dromara.autocode.service.SysAutoCodeRuleService;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.dromara.autocode.utils.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 编码生成规则Service业务层处理
 *
 * @author wenrui Su
 * @date 2025-03-19
 */
@RequiredArgsConstructor
@Service
public class SysAutoCodeRuleServiceImpl  extends BaseService implements SysAutoCodeRuleService{

    private final SysAutoCodeRuleMapper baseMapper;

    /**
     * 查询编码生成规则
     */
    public SysAutoCodeRule queryById(Long ruleId){
        return baseMapper.selectById(ruleId);
    }

    /**
     * 查询编码生成规则列表
     */
    public Page<SysAutoCodeRule> queryPageList(SysAutoCodeRule bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysAutoCodeRule> lqw = buildQueryWrapper(bo);
        Page<SysAutoCodeRule> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return result;
    }

    /**
     * 查询编码生成规则列表
     */
    public List<SysAutoCodeRule> queryList(SysAutoCodeRule bo) {
        LambdaQueryWrapper<SysAutoCodeRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysAutoCodeRule> buildQueryWrapper(SysAutoCodeRule bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysAutoCodeRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRuleCode()), SysAutoCodeRule::getRuleCode, bo.getRuleCode());
        lqw.like(StringUtils.isNotBlank(bo.getRuleName()), SysAutoCodeRule::getRuleName, bo.getRuleName());
        lqw.eq(StringUtils.isNotBlank(bo.getRuleDesc()), SysAutoCodeRule::getRuleDesc, bo.getRuleDesc());
        lqw.eq(bo.getMaxLength() != null, SysAutoCodeRule::getMaxLength, bo.getMaxLength());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPadded()), SysAutoCodeRule::getIsPadded, bo.getIsPadded());
        lqw.eq(StringUtils.isNotBlank(bo.getPaddedChar()), SysAutoCodeRule::getPaddedChar, bo.getPaddedChar());
        lqw.eq(StringUtils.isNotBlank(bo.getPaddedMethod()), SysAutoCodeRule::getPaddedMethod, bo.getPaddedMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getEnableFlag()), SysAutoCodeRule::getEnableFlag, bo.getEnableFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getAttr1()), SysAutoCodeRule::getAttr1, bo.getAttr1());
        lqw.eq(StringUtils.isNotBlank(bo.getAttr2()), SysAutoCodeRule::getAttr2, bo.getAttr2());
        lqw.eq(bo.getAttr3() != null, SysAutoCodeRule::getAttr3, bo.getAttr3());
        lqw.eq(bo.getAttr4() != null, SysAutoCodeRule::getAttr4, bo.getAttr4());
        return lqw;
    }

    /**
     * 新增编码生成规则
     */
    public Boolean insertByBo(SysAutoCodeRule bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        return flag;
    }

    /**
     * 修改编码生成规则
     */
    public Boolean updateByBo(SysAutoCodeRule bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysAutoCodeRule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除编码生成规则
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

  @Autowired
  private SysAutoCodeRuleMapper sysAutoCodeRuleMapper;

  @Override
  public SysAutoCodeRule getOne(String ruleCode) {
    SysAutoCodeRule param = new SysAutoCodeRule();
    param.setRuleCode(ruleCode);
    List<SysAutoCodeRule> rules = sysAutoCodeRuleMapper.selectSysAutoCodeResultList(param);
    if(CollectionUtil.isNotEmpty(rules)){
      return rules.get(0);
    }
    return null;
  }

  @Override
  public List<SysAutoCodeRule> selectAutoCodeList(SysAutoCodeRule sysAutoCodeRule) {
    return sysAutoCodeRuleMapper.selectSysAutoCodeResultList(sysAutoCodeRule);
  }

  @Override
  public SysAutoCodeRule findById(Long ruleId) {
    return sysAutoCodeRuleMapper.findById(ruleId);
  }

  @Override
  public String checkRuleCodeUnique(SysAutoCodeRule sysAutoCodeRule) {
    SysAutoCodeRule rule = sysAutoCodeRuleMapper.checkRuleCodeUnique(sysAutoCodeRule.getRuleCode());
    if (StringUtils.isNotNull(rule) && rule.getRuleId().longValue() != sysAutoCodeRule.getRuleId().longValue())
    {
      return UserConstants.NOT_UNIQUE;
    }

    return UserConstants.UNIQUE;
  }

  @Override
  public String checkRuleNameUnique(SysAutoCodeRule sysAutoCodeRule) {
    SysAutoCodeRule rule = sysAutoCodeRuleMapper.checkRuleNameUnique(sysAutoCodeRule.getRuleName());
    if (StringUtils.isNotNull(rule) && rule.getRuleId().longValue() != sysAutoCodeRule.getRuleId().longValue())
    {
      return UserConstants.NOT_UNIQUE;
    }

    return UserConstants.UNIQUE;
  }

  @Override
  public int insertInfo(SysAutoCodeRule rule) {
    return sysAutoCodeRuleMapper.add(rule);
  }

  @Override
  public int updateInfo(SysAutoCodeRule rule) {
    return sysAutoCodeRuleMapper.updateSysAutoCodeRule(rule);
  }

  @Override
  public int deleteById(Long ruleId) {

    return sysAutoCodeRuleMapper.deleteById(ruleId);
  }

  @Override
  public int deleteByIds(Long[] ruleIds) {
    for (Long ruleId: ruleIds
    ) {
      sysAutoCodeRuleMapper.deleteById(ruleId);
    }
    return 1;
  }
}
